<?php /** Register Sidebars */

add_action( 'widgets_init', 'theme_register_sidebars' );

function theme_register_sidebars()
{	
	global $_webnukes;
	/** Blog Sidebar */
	register_sidebar(
		array( 'id' => 'homepage-sidebar1',
			'name' => __( 'Homepage Sidebar 1', THEME_NAME ),
			'description' => 'Widgets in this area will be shown on the Blog Posts Pages.',
			'before_widget' => '<div id="%1$s" class="block %2$s">',
			'after_widget' => '</div>',
			'before_title' => '<div class="heading">',
			'after_title' => '</div>'
		)
	);
	
	/** Blog Sidebar */
	register_sidebar(
		array( 'id' => 'homepage-sidebar2',
			'name' => __( 'Homepage Sidebar 2', THEME_NAME ),
			'description' => 'Widgets in this area will be shown on the Blog Posts Pages.',
			'before_widget' => '<div id="%1$s" class="block %2$s">',
			'after_widget' => '</div>',
			'before_title' => '<div class="heading">',
			'after_title' => '</div>'
		)
	);
	
	/** Blog Sidebar */
	register_sidebar(
		array( 'id' => 'homepage-sidebar3',
			'name' => __( 'Homepage Sidebar 3', THEME_NAME ),
			'description' => 'Widgets in this area will be shown on the Blog Posts Pages.',
			'before_widget' => '<div id="%1$s" class="block %2$s">',
			'after_widget' => '</div>',
			'before_title' => '<div class="heading">',
			'after_title' => '</div>'
		)
	);
	
	/** Blog Sidebar */
	register_sidebar(
		array( 'id' => 'homepage-sidebar4',
			'name' => __( 'Homepage Sidebar 4', THEME_NAME ),
			'description' => 'Widgets in this area will be shown on the Blog Posts Pages.',
			'before_widget' => '<div id="%1$s" class="block %2$s">',
			'after_widget' => '</div>',
			'before_title' => '<div class="heading">',
			'after_title' => '</div>'
		)
	);
	
	/** Blog Sidebar */
	register_sidebar(
		array( 'id' => 'blog-sidebar',
			'name' => __( 'Blog Sidebar', THEME_NAME ),
			'description' => 'Widgets in this area will be shown on the Blog Posts Pages.',
			'before_widget' => '<div id="%1$s" class="block %2$s">',
			'after_widget' => '</div>',
			'before_title' => '<div class="heading">',
			'after_title' => '</div>'
		)
	);

	/** Footer Top */
	register_sidebar(
		array( 'id' => 'footer-top',
			'name' => __( 'Footer Top', THEME_NAME ),
			'description' => 'Widget in this area will be shown in the top of footer.',
			'before_widget' => '<div id="twiterfeed">',
			'after_widget' => '</div>',
			'before_title' => '<div class="leftside">',
			'after_title' => '</div>'
		)
	);

	/** Footer Sidebar 1 */
	register_sidebar(
		array( 'id' => 'footer-sidebar1',
			'name' => __( 'Footer Sidebar 1', THEME_NAME ),
			'description' => 'Widgets in this area will be shown in the left of footer.',
			'before_widget' => '<div class="block flickr_sect">',
			'after_widget' => '</div>',
			'before_title' => '<div class="title"><h1>',
			'after_title' => '</h1></div>'
		)
	);

	/** Footer Sidebar 2 */
	register_sidebar(
		array( 'id' => 'footer-sidebar2',
			'name' => __( 'Footer Sidebar 2', THEME_NAME ),
			'description' => 'Widgets in this area will be shown in the center left of footer.',
			'before_widget' => '<div class="block p-care dotted">',
			'after_widget' => '</div>',
			'before_title' => '<div class="title"><h1>',
			'after_title' => '</h1></div>'
		)
	);

	/** Footer Sidebar 3 */
	register_sidebar(
		array( 'id' => 'footer-sidebar3',
			'name' => __( 'Footer Sidebar 3', THEME_NAME ),
			'description' => 'Widgets in this area will be shown in the center right of footer.',
			'before_widget' => '<div class="block getintouch">',
			'after_widget' => '</div>',
			'before_title' => '<div class="title"><h1>',
			'after_title' => '</h1></div>'
		)
	);

	/** Footer Sidebar 4 */
	register_sidebar(
		array( 'id' => 'footer-sidebar4',
			'name' => __( 'Footer Sidebar 4', THEME_NAME ),
			'description' => 'Widgets in this area will be shown in the right of footer.',
			'before_widget' => '<div class="block newsletter last-child">',
			'after_widget' => '</div>',
			'before_title' => '<div class="title"><h1>',
			'after_title' => '</h1></div>'
		)
	);
	
	/** Create dynamic sidebars */
	$settings = get_option(THEME_PREFIX.'sidebar_creator');
	
	if($_webnukes->kvalue($settings,'status') == 'active'){
		if($_webnukes->kvalue($settings,'DYNAMIC')){
			
			foreach($_webnukes->kvalue($settings,'DYNAMIC') as $key=>$val)
			{
				$name = ($_webnukes->kvalue($val,'name')) ? $val['name'] : __('Dynamic Sidebar ', THEME_NAME).$key;
				
				if($_webnukes->kvalue($val,'template') == 'mega_menu') $id = ($_webnukes->kvalue($val,'id')) ? 'megamenu-'.$val['id'] : 'megamenu-'.$key;
				else $id = ($_webnukes->kvalue($val,'id')) ? $val['id'] : 'dynamic-sidebar'.$key;
				

				$before_title = ($_webnukes->kvalue($val, 'template') == 'mega_menu') ? '<h3>' : '<div class="title"><h1>';
				$after_title = ($_webnukes->kvalue($val, 'template') == 'mega_menu') ? '</h3>' : '</h1></div>';
				$class = ($_webnukes->kvalue($val, 'class')) ? $_webnukes->kvalue($val, 'class') : 'block';

				
				/** Register Dynamic Sidebar */
				register_sidebar(
					array( 'id' => $id,
						'name' => $name,
						'before_widget' => '<div id="%1$s" class="'.$class.' %2$s">',
						'after_widget' => '</div>',
						'before_title' => $before_title,
						'after_title' => $after_title
					)
				);
			}
		}
	}
}


/**
 * Function is used to load dynamically created sidebars
 *
 * @param	string		$tpl_name		Name of WP template file where do you want to show the sidebar
 * @param	string		$default		id/Name of the default sidebar if provided $tpl_name doesn't find.
 * @return	string						prints the dyanmic created html of sidebar if found else prints nothing
 */
function _load_dynamic_sidebar($tpl_name, $default = 'blog-sidebar')
{
	global $_webnukes;
	
	$settings = get_option(THEME_PREFIX.'sidebar_creator');
			
	if($_webnukes->kvalue($settings, 'status') == 'active')
	{
		$tmpls = array_reduce( (array)$_webnukes->kvalue($settings, 'DYNAMIC'), create_function('$new_tpl, $v', '$new_tpl[$v["template"]] = $v; return $new_tpl;') );

		if($sidebar = $_webnukes->kvalue($tmpls, $tpl_name)) 
		{
			if(is_active_sidebar($_webnukes->kvalue($sidebar, 'id'))) dynamic_sidebar($sidebar['id']);
		}else dynamic_sidebar($default);
	}else dynamic_sidebar($default);
}
?>